package com.demo.userprofile.component.service;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;

import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;


/**
 * 连接ClickHouse测试
 *
 * @author userprofile_demo
 */
@Lazy
@Service
public class ClickHouseService {
    private static Connection connection = null;

    static {
        try {
            Class.forName("ru.yandex.clickhouse.ClickHouseDriver");
            String url = "jdbc:clickhouse://userprofile-master:8123";
            String user = "";
            String password = "";
            connection = DriverManager.getConnection(url, user, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws SQLException {
        Statement statement = connection.createStatement();
        ResultSet resultSet = statement.executeQuery("select * from system.functions");
        ResultSetMetaData metaData = resultSet.getMetaData();
        int columnCount = metaData.getColumnCount();
        while (resultSet.next()) {
            for (int i = 1; i <= columnCount; i++) {
                System.out.println(metaData.getColumnName(i) + ":" + resultSet.getString(i));
            }
        }
    }
}
